
<%@page import="com.guestmanage.servlet.util.XDEncodeHelper"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.spmanage.util.HttpUtil"%>
<%@page import="com.spmanage.util.DateUtil"%>
<%@page import="com.spmanage.util.BillingUtil"%><%@page import="com.spmanage.util.DbOperation"%>
<%@page import="com.spmanage.util.SqlUtil"%><%@page import="com.spmanage.util.WebAction"%>
<%@page import="com.spmanage.util.StringUtil"%><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%><%
    System.out.println("广典接口-报告同步");
	WebAction webAction = new WebAction(request,response);
	
	String encodeStr = webAction.getParameterString("encodeStr");//使用经过讯典定义的函数编码后的字符串
	String key = "tsqRDDdHVgPx7SJh3tQnz1b0"; //双方约定的KEY(密钥),用来加密,解密
	String returnStr="";
	encodeStr=StringUtil.encodeStringByUTF8(encodeStr);
    String date = DateUtil.nowTimeTransformCharacter(DateUtil.getNow_Timestamp().toString());
	if(encodeStr!=null){
		returnStr = "ok" ;
		
		//调用解码函数进行解码
		XDEncodeHelper xdHelper = new XDEncodeHelper(key);
		encodeStr = xdHelper.XDDecode(encodeStr,true);
		System.out.println(encodeStr);
		String strReplace = encodeStr.replace('$',',');//格式如：13478351349$0000018540$Dyk1234哈哈$22080538$286$1$1
		String[] strArr = strReplace.split(",");
		
		String linkId = strArr[0];//上下行关联标识
		String status = strArr[1];//DELIVRD
		String userPhone = strArr[2];//手机号码
		//String mo = strArr[3];//上行内容
		
		//String fee = webAction.getParameterString("fee");

		
		String org = SqlUtil.getStringResult("select org from "+BillingUtil.getSendDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
	    if(org!=null&&org.length()>0) {
	    	System.out.println("该linkid已经有同步状态");
	    	return;
	    }
	    
	    String spNo = SqlUtil.getStringResult("select dest from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);
	    String mo = SqlUtil.getStringResult("select body from "+BillingUtil.getRecvDay()+" where linkid = '"+linkId+"'",DbOperation.DB_STAT);

		//指令处理,返回处理后的指令
		String strMo = BillingUtil.getMo(spNo, mo);
	    //获取用户渠道号
	    int channelId = BillingUtil.getUserChannel(spNo,strMo);
	    int spid = BillingUtil.getUserSpId(spNo,strMo);
	    //获取是否要扣量
		int ischeck = BillingUtil.getCheckSend(linkId);
	    int provinceId = BillingUtil.getProvinceId(userPhone);
	    
		//通过端口和指令获取资费
		String fee = String.valueOf(BillingUtil.getSpFee(spNo, strMo));
	    
	    StringBuilder sb = new StringBuilder();
	    sb.append("'");
	    sb.append(spid);
	    sb.append("',");
	    sb.append("1");//msgmode
	    sb.append(",");
	    sb.append("15");//fmt
	    sb.append(",");
	    sb.append("0");//futype
	    sb.append(",");
	    sb.append("2");//ftype
	    sb.append(",");
	    sb.append(fee);//feecode  
	    sb.append(",");
	    sb.append("'");
	    sb.append("");//stype
	    sb.append("',");
	    sb.append("1");//num
	    sb.append(",");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("'");
	    sb.append(spNo);
	    sb.append("',");
	    sb.append("'");
	    sb.append(userPhone);
	    sb.append("',");
	    sb.append("getDate()");  //tm
	    sb.append(",");
	    sb.append("'");
	    sb.append("mid");  //mid
	    sb.append("',");
	    sb.append("'");
	    sb.append("msgid");  //msgid
	    sb.append("',");
	    sb.append("0");//result  DELIVRD
	    sb.append(",");
	    sb.append("'");
	    sb.append(status);//DELIVRD
	    sb.append("',");
	    sb.append("'");
	    sb.append(channelId);//channelId
	    sb.append("',");
	    sb.append("'");
	    sb.append(linkId);//linkid
	    sb.append("','"+mo+"'");
	    sb.append(",");
	    sb.append(provinceId);
	    sb.append(",");
	    sb.append(ischeck);
	        
	    SqlUtil.executeUpdate("insert into "+BillingUtil.getSendDay()+"(areaid,msgMode,fmt,futype,ftype,feecode,stype,num,org,dest,fee,tm,mid,msgid,result,stat,infoid,linkid,body,province,ischeck) values("+sb.toString()+")",DbOperation.DB_STAT);
		
	    System.out.println("广典接口-状态是否需要扣量："+ischeck+",linkid:"+linkId);
	    if(ischeck == 0) {
	    	System.out.println("广典-状态同步:"+SqlUtil.getStringResult("select cpname from adminuser where id = "+ channelId, 5));
	    	String http = BillingUtil.getMtUrlByChannel(channelId);
	    	if(http!=null&&http.length()>0) {
	    		StringBuilder moUrl = new StringBuilder(http);
	    		if(moUrl.indexOf("?")>0){
	    			moUrl.append("&userPhone=");
	    		}else{
	    			moUrl.append("?userPhone=");
	    		}
		    	moUrl.append(userPhone);
		    	moUrl.append("&spNo=");
		    	moUrl.append(spNo);
		    	moUrl.append("&linkId=");
		    	moUrl.append(linkId);
		    	moUrl.append("&mo=");
		    	moUrl.append(URLEncoder.encode(mo,"UTF-8"));
		    	moUrl.append("&date=");
		    	moUrl.append(URLEncoder.encode(date,"UTF-8"));
		    	moUrl.append("&fee=");
		    	moUrl.append(fee);
		    	moUrl.append("&status=");
		    	moUrl.append(status);
			    System.out.println("广典报告同步地址："+moUrl.toString());
			    HttpUtil.getHtmlSource(moUrl.toString(),"UTF-8");
	    	}
	    }
				
	}else{
		returnStr = "err" ;
	}
    response.getWriter().write(returnStr);
%>
